#!/usr/bin/perl

=head1 NAME

AFX::Reset - Delete current ARF, logs.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010 Mikhail Volkov

This file is part of AuditFX.

    AuditFX is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AuditFX is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AuditFX.  If not, see <http://www.gnu.org/licenses/>.
=cut

##############################################################################
# Globals
#

use lib 'lib';
use strict;
use warnings;

use File::Path;
use File::Copy;

use AFX::ACF;

## Execute main script
audit_reset();

##############################################################################
#
# audit_reset()

sub audit_reset {

## Open ACF
    my $acf = new AFX::ACF();

## Print version message
    print $acf->message_title() . "\n\n";

## Delete current ARF
    my $arfdir = $acf->get_directory('current');
    rmtree($arfdir) if $arfdir and -d $arfdir;

## Get Template directory
    my $tpldir = $acf->get_directory('template');

## Open directory for removing HTML/GV for Templates
    if ( opendir( DIR, $tpldir ) ) {

## Find ARF
        my @tpls = grep( /\.(gv|html)$/, readdir(DIR) );

## Unlinkall
        foreach my $tmpfile (@tpls) {

## Remove file
            unlink("$tpldir/$tmpfile");
        }

## Close directory
        closedir(DIR);
    }

## Get Scenario directory
    $tpldir = $acf->get_directory('scenario');

## Open directory for removing HTML for Scenario
    if ( opendir( DIR, $tpldir ) ) {

## Find ARF
        my @tpls = grep( /\.html$/, readdir(DIR) );

## Unlinkall
        foreach my $tmpfile (@tpls) {

## Remove file
            unlink("$tpldir/$tmpfile");
        }

## Close directory
        closedir(DIR);
    }

## Delete all logs
    rmtree('log');

## Set Error status
    my $status = 0;

## Get time
    my $now = localtime;

## Set message
    my $message = "## ARF and Log resetting finished at $now ";
    $message .= "#" while ( length($message) < 70 );
    $message .= "\n";

## Show Status
    print $message. "STATUS=$status\nINFO=No issues detected during reset\n";

## Exit with status
    exit($status);

} ## end sub audit_reset
